#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_btnConnect_clicked()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName(ui->leHostName->text());
    db.setPort(ui->lePort->text().toInt());
    db.setDatabaseName(ui->leDatabase->text());
    db.setUserName(ui->leUsername->text());
    db.setPassword(ui->lePassword->text());
    bool ok = db.open();
    if (ok){
        QMessageBox::information(this, "链接成功", "成功连接数据库");
    }
    else {
        QMessageBox::critical(this, "错误", QString("连接数据库失败！%1").arg(db.lastError().text()));
    }
}
